<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="layui/css/layui.css" media="all" rel="stylesheet">
    <script src="layui/layui.js" type="text/javascript"></script>
    <script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
</head>
<body>
<div class="layui-container" style="margin-top: 50px;">

    <div class="layui-container" style="width: 500px;height: 330px;padding-top: 60px;">
        <form action="" class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">原密码：</label>
                <div class="layui-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="old_password"
                           placeholder="请输入旧密码" type="password">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码：</label>
                <div class="layui-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="new_password"
                           placeholder="请输入新密码" type="password">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码：</label>
                <div class="layui-inline">
                    <input autocomplete="off" class="layui-input" lay-verify="required" name="confirm_password"
                           placeholder="请再次输入新密码" type="password">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-filter="demo2" lay-submit="" style="margin-left: 160px;">确认修改</button>
            </div>
        </form>
    </div>
</div>
<script charset="utf-8" src="layui/layui.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form;

        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length == 0) {
                    return '用户名不能为空';
                }
            }
            , password: [/(.+){6,12}$/, '密码必须6到12位']
        });
        form.on('submit(demo2)', function (data) {
            var old=data.field.old_password;
            var newPassword=data.field.new_password;
            var confirm=data.field.confirm_password;
            if(old === newPassword ){
                alert("新密码与旧密码不得相同！");
                return false;
            }
            if (newPassword !== confirm){
                alert("两次密码输入不一致");
                return false;
            }
            //向后台发送数据，验证登录信息
            $.ajax({
                url: '/account/editPassword/teacher' , //请求url
                data: {
                    "username":sessionStorage.getItem("username"),
                    "newPassword":newPassword,
                    "oldPassword":old
                }, //请求参数，类似于username=xxx&password=xxx
                type: 'put', //请求方式
                dataType: 'json', //返回值的类型
                success: function (result) {  //服务器成功返回时执行的函数
                    console.log(JSON.stringify(result));
                    if (result.code == 2000) {
                        alert("密码修改成功，请重新登陆！");
                        sessionStorage.clear();
                        top.location.href="login.html";
                    } else {
                        alert(result.msg);
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>